﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace YXShop.Web.admin.store
{
    public partial class storecenter_contact_edit : System.Web.UI.Page
    {
        YXShop.BLL.Store.StoreCenter storeCenterBll = new YXShop.BLL.Store.StoreCenter();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int id = ChangeHope.WebPage.PageRequest.GetQueryInt("id");
                if (ChangeHope.WebPage.PageRequest.GetQueryString("iscenter") == "1")
                {
                    List<YXShop.Model.Store.StoreCenter> sc = storeCenterBll.GetListShopByWhere(" and iscenter=1");
                    if (sc.Count > 0)
                    {
                        id = sc[0].Sid.Value;
                    }
                }
                InitWebControl();
                if (id > 0)
                {
                    GetStoreCenter(id);
                }
            }
        }

        private void GetStoreCenter(int id)
        {
            YXShop.Model.Store.StoreCenter sc = new Model.Store.StoreCenter();
            YXShop.BLL.Admin.Administrators admin = new BLL.Admin.Administrators();
            sc = storeCenterBll.GetModelById(id);
            lblName.Text = sc.StoreCentername;
            lblAdminName.Text = admin.GetModel(sc.Adminid.Value).Name;
            lblType.Text = YXShop.Common.ValuePair.GetStoreType(sc.StoreCentertype.Value);
            txtAddress.Text = sc.Address;
            txtZipCode.Text = sc.Zipcode;
            txtMobilePhone.Text = sc.Mobilephone;
            txtPhone.Text = sc.Phone;
            txtContact.Text = sc.Contact;
            txtFax.Text = sc.Fax;
        }

        #region 验证
        private void InitWebControl()
        {
            bool isEdit=true;
            if (ChangeHope.WebPage.PageRequest.GetQueryString("isedit") == "1")
                isEdit = true;
            else
                isEdit = false;
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            if (isEdit)
            {

                ChangeHope.WebPage.WebControl.Validate(this.txtZipCode, "邮政编码", "ispostalcode", "需符合邮政编码格式", "该项需符合邮政编码格式");
                ChangeHope.WebPage.WebControl.Validate(this.txtFax, "传真", "isfaxorphone", "需符合传真号码格式", "该项需符合传真号码格式");
                ChangeHope.WebPage.WebControl.Validate(this.txtMobilePhone, "手机号码", "ismobilephone", "需符合手机号码格式", "该项需符合手机号码格式");
                ChangeHope.WebPage.WebControl.Validate(this.txtPhone, "电话", "isfaxorphone", "需符合电话号码格式", "该项需符合电话号码格式");
            }

            txtAddress.ReadOnly = !isEdit;
            txtZipCode.ReadOnly = !isEdit;
            txtContact.ReadOnly = !isEdit;
            txtFax.ReadOnly = !isEdit;
            txtMobilePhone.ReadOnly = !isEdit;
            txtPhone.ReadOnly = !isEdit;
            btnSave.Visible = isEdit;
            Button1.Visible = isEdit;
        }
        #endregion

        #region 保存数据
        protected void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }


        private void Save()
        {
            int id = ChangeHope.WebPage.PageRequest.GetQueryInt("id");
            if (ChangeHope.WebPage.PageRequest.GetQueryString("iscenter") == "1")
            {
                List<YXShop.Model.Store.StoreCenter> sc = storeCenterBll.GetListShopByWhere(" and iscenter=1");
                if (sc.Count > 0)
                {
                    id = sc[0].Sid.Value;
                }
            }
            
            YXShop.BLL.Admin.Administrators adminBll = new YXShop.BLL.Admin.Administrators();
            YXShop.Model.Store.StoreCenter storecenter = storeCenterBll.GetModelById(id);

            storecenter.Updatetime = DateTime.Now;
            storecenter.Address = txtAddress.Text;
            storecenter.Zipcode = txtZipCode.Text;
            storecenter.Mobilephone = txtMobilePhone.Text;
            storecenter.Phone = txtPhone.Text;
            storecenter.Contact = txtContact.Text;
            storecenter.Fax = txtFax.Text;

            int count=0;
            if (id > 0)
            {
                storecenter.Sid = id;
                count = storeCenterBll.Update(storecenter);
            }
            if (count > 0)
            {
                if (id > 0)
                {
                    this.ltlMsg.Text = "操作成功，已修改该信息";
                    Response.Redirect(Request.Url.ToString());
                }
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionOk";
            }
            else
            {
                this.ltlMsg.Text = "操作失败";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionErr";
            }

        }
        #endregion



    }
}
